<?php

namespace app\event;

use app\common\service\pay\alipay\AliPayBase;
use app\common\service\pay\PayService;
use app\common\service\pay\wechat\WechatBase;
use app\model\CouponOrder;
use app\model\PayLog;
use think\facade\Log;

class OrderPayRefund
{
    /**
     * @var CouponOrder $couponOrder
     */
    public $couponOrder;

    public function __construct(CouponOrder $couponOrder)
    {
        $this->couponOrder = $couponOrder;

        $payLog = $couponOrder->payLog;



        //退款逻辑
        //查找到支付渠道
        if (empty($payLog)) {
            Log::error('退款没有退款渠道');
            return false;
        }

        if ($payLog->status != PayLog::STATUS_PAY){
            Log::error('退款状态错误');
            return  false;
        }


        $class = (new PayLog())->payMethondConvert($payLog->trade_type);

        if (empty($class)) {
            Log::error('没有相应的支付渠道类型');
            return false;
        }
        /**
         * @var WechatBase
         * @var AliPayBase
         */
        (new PayService($couponOrder))->refund(new $class());



    }

}